package la.iok.hzsvn.lewin.movie.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.RoomVo;
import la.iok.hzsvn.lewin.movie.entity.InnerTestMovie;
import la.iok.hzsvn.lewin.movie.entity.Movie;
import la.iok.hzsvn.lewin.movie.mapper.InnerTestMovieMapper;
import la.iok.hzsvn.lewin.movie.model.MovieInnerTestVo;
import la.iok.hzsvn.lewin.movie.services.MovieServiceInvoker;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.collection.IdOperators;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class InnerTestMovieServiceImpl extends BaseServiceImpl<InnerTestMovie, InnerTestMovieMapper,Long> implements InnerTestMovieService {
    private final MovieServiceInvoker movieServiceInvoker;
    private final MovieService movieService;
    public InnerTestMovieServiceImpl(
            InnerTestMovieMapper mapper,
            MovieServiceInvoker movieServiceInvoker,
            MovieService movieService) {
        super(mapper);
        this.movieServiceInvoker = movieServiceInvoker;
        this.movieService = movieService;
    }

    @Override
    public Long save(Long movieId, Long roomId) {
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(movieId != null && movieId > 0,"剧本","剧本id必须大于0");
        RoomVo room = movieServiceInvoker.roomInfo(roomId);
        ErrorCode.ROOM_NOT_EXIST.assertNotNull(room,roomId);

        InnerTestMovie entity = listByProperty("movieId",movieId).stream().filter(item-> Objects.equals(roomId,item.getRoomId())).findFirst().orElse(null);
        if(entity != null){
            return entity.getId();
        } else {
            entity = new InnerTestMovie();
            entity.setMovieId(movieId);
            entity.setOrgUnitId(room.getOrgUnitId());
            entity.setRoomId(roomId);
            insertSelective(entity);
            return entity.getId();
        }
    }

    @Override
    public PageVo<MovieInnerTestVo> page(int page, int limit, Long movieId, @Nullable Long orgUnitId, Long roomId) {
        Page<InnerTestMovie> list = PageHelper.startPage(page,limit);
        mapper.list(movieId, orgUnitId, roomId);
        Set<Long> movieIds = list.stream().map(InnerTestMovie::getMovieId).filter(Objects::nonNull).collect(Collectors.toSet());
        Set<Long> roomIds = list.stream().map(InnerTestMovie::getRoomId).collect(Collectors.toSet());
        List<RoomVo> rooms = movieServiceInvoker.roomInfos(roomIds);
        List<Movie> movies = movieService.listInProperty("id",movieIds);
        return page(list,entity->{
            MovieInnerTestVo vo = new MovieInnerTestVo();
            BeanUtils.copyProperties(entity,vo);
            RoomVo r = IdOperators.find(rooms,entity.getRoomId());
            if(r != null){
                vo.setRoomName(r.getName());
                vo.setOrgUnitName(r.getOrgUnitName());
            }
            Movie m = IdOperators.find(movies,entity.getMovieId());
            if(m != null){
                vo.setMovieName(m.getName());
            }
            return vo;
        });
    }
}
